package com.rfid.rfidbluetooth;

import com.rfid.base.util.ByteUtils;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class CmdBuild {

    public static short CMD_REQ_RFID = (short) 0XF005;

    public static short CMD_M_EPC = (short) 0XF006;
    public static byte[] reqRfid(int shelf,int pn) {
        ByteBuffer bb = ByteBuffer.allocate(7);
        bb.putShort(CMD_REQ_RFID);
        bb.put((byte) shelf);
        bb.put((byte) pn);
        bb.put((byte) 0x00);
        bb.put((byte) 0);
        byte m = (byte) ((((CMD_REQ_RFID>>8)&0xff) + (CMD_REQ_RFID&0XFF)  + shelf + pn)&0xFF);
        System.out.println("m = " + m);
        bb.put(m);
        byte data[] = bb.array();
        return data;
    }

    public static byte[] mockResponEpcs(int ph) {
        ByteBuffer bb = ByteBuffer.allocate(1024);
        short cmd = CMD_REQ_RFID;
        System.out.println("writeRfids cmd = " + Integer.toHexString(cmd&0XFFFF));
        bb.putShort(cmd);
        bb.put((byte) 1);
        bb.put((byte) ph);
        for (int i=0;i<12;i++) {
            bb.put((byte) (i+1));
            byte epc[] = ByteUtils.hexStr2Byte("E00401002BB1BD2" + (i<10?i:"A"));
            bb.put((byte) epc.length);
            bb.put(epc);
        }
        //校验位
        bb.put((byte) 0);
        bb.flip();
        byte data[] = new byte[bb.remaining()];
        bb.get(data);
        return data;
    }

    public static byte[] modifyEpc(int shelf,int ph,int port,String epc) {
        byte epcby[] = ByteUtils.hexString2Bytes(epc);
        ByteBuffer bb = ByteBuffer.allocate(7 + epcby.length);
        bb.putShort(CMD_M_EPC);
        bb.put((byte) (shelf&0XFF));
        bb.put((byte) (ph&0XFF));
        bb.put((byte) port);
        bb.put((byte) epcby.length);
        bb.put(epcby);
        byte m = (byte) ((((CMD_M_EPC>>8)&0xff) + (CMD_M_EPC&0XFF) + shelf + ph + port + epcby.length)&0xff);
        for (byte b : epcby) {
            m = (byte) (m + b);
        }
        m = (byte) (m&0xff);
        bb.put(m);
        byte data[] = bb.array();
        return data;
    }

    public static boolean readModifyEpcResualt(DataInputStream inputStream) throws IOException {
        int shelf = inputStream.read();
        int ph = inputStream.read();
        int port = inputStream.read();
        int status = inputStream.read();
        int v = inputStream.read();
        return status == 0x01;
    }

    public static List<Rfid> readRfids(DataInputStream inputStream) throws IOException {
        int cell = inputStream.read();
        int ph = inputStream.read();
        List<Rfid> rfids = new ArrayList<>();
        for (int i=0;i<12;i++) {
            Rfid rfid = new Rfid();
            int port = inputStream.read();
            System.out.println("port = " + port);
            int len = inputStream.read();
            byte buffer[] = new byte[len];
            if (len > 0) {
                inputStream.readFully(buffer,0,len);
                System.out.println("parse = " + ByteUtils.bytesToHexString(buffer).toUpperCase());
                rfid.setEpc(ByteUtils.bytesToHexString(buffer).toUpperCase());
            }
            rfid.setPh(ph);
            rfid.setPort(port);
            rfid.setShelf(cell);
            rfids.add(rfid);
        }
        //校验位
        inputStream.read();
        return rfids;
    }

    public static byte[] sendEpcs(List<Rfid> rfids) {
        ByteBuffer bb = ByteBuffer.allocate(1024);
        bb.put((byte) 0x1A);
        bb.put((byte) rfids.get(0).getShelf());
        bb.put((byte) rfids.get(0).getPh());
        for (Rfid rfid : rfids) {
            bb.put((byte) rfid.getPort());
            byte data[] = ByteUtils.hexStr2Byte(rfid.getEpc());
            bb.put((byte) data.length);
            bb.put(data);
        }
        bb.putInt(0);
        bb.flip();
        byte buffer[] = new byte[bb.remaining()];
        bb.get(buffer);
        return buffer;
    }


}
